/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdVerUsuarios.java
 *
 * Created on 13-ago-2010, 10:15:51
 */

package nuevoTrabajador;

import delegate.GestionSeguridadDelegate;

import com.jidesoft.hints.ListDataIntelliHints;
import delegate.GestionDelegate;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import seguridad.administracion.ejb30.TO.UsuarioTO;
import suministros.ejb30.entity.Persona;
import suministros.ejb30.entity.Trabajador;
import utiles.Utiles_MensajesDialogo;



/**
 *
 * @author Luis_Angel
 */
public class jdVerTrabajadores extends javax.swing.JDialog {

     private JTableBinding jTableBinding;
     private List<UsuarioTO> trabajadores;
     private List<UsuarioTO> trabajadores_temp;
    private BindingGroup bindingGroup;
    private ListDataIntelliHints intelliHintsTrabajador;
 
    /** Creates new form jdVerUsuarios */
    public jdVerTrabajadores(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        trabajadores_temp = new ArrayList(1);
        initComponents();
        try {
            this.trabajadores = GestionSeguridadDelegate.getInstance().getListaTrabajadores();
            trabajadores_temp = this.trabajadores;
            this.actualizaTablaTrabajadores(this.trabajadores);
            this.cargaHintsIndependientes();
        } catch (Exception ex) {
            Logger.getLogger(jdVerTrabajadores.class.getName()).log(Level.SEVERE, null, ex);
        }
        pack();
    }

    private void cargaHintsIndependientes() throws Exception {
         this.intelliHintsTrabajador = new ListDataIntelliHints(this.jtfTrabajador,trabajadores);
    }

    private UsuarioTO getTrabajadorcodigo(String dni){
         UsuarioTO retorno = null;
        for (UsuarioTO user : this.trabajadores) {
          if (user.getDni().equalsIgnoreCase(dni)) {
            retorno = user;
            break;
          }
        }
        return retorno;
    }

    private void buscarTrabajador()
      {
        UsuarioTO pro;
        if (this.jtfDni.getText().trim().length() < 1) {
          try { pro = (UsuarioTO)this.intelliHintsTrabajador.getSelectedHint();
          } catch (NullPointerException ex) {
            pro = null;
          }
        } else {
          pro = this.getTrabajadorcodigo(this.jtfDni.getText().trim());
        }
        if (pro == null) {
          Utiles_MensajesDialogo.mensajeError("El trabajador no existe!");
          this.jtfDni.setText("");
          pro = null;
        } else {
          this.jtfTrabajador.setText(pro.getNombre()+ " - " + pro.getDni());
          this.jtfDni.setText(pro.getDni());
          this.trabajadores_temp.clear();
          this.trabajadores_temp.add(pro);
          this.actualizaTablaTrabajadores(this.trabajadores_temp);
       //   this.ruc_proveedor=pro.getCodProveedor();
            try {
           //     this.detalles_solicitud = GestionPedidosDelegate.getInstance().getDetallesSolicitud(pro.getCodProveedor(), this.nro_solicitud);
           //     actualizaTablaDetallesSolicitud();
            } catch (Exception ex) {
                Logger.getLogger(jdVerTrabajadores.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

     private void actualizaTablaTrabajadores(List<UsuarioTO> trabajadores_find) {
            this.bindingGroup = new BindingGroup();
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, trabajadores_find, this.jtTrabajadores);

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${dni}"));

            columnBinding0.setColumnName("DNI");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${nombre}"));

            columnBinding1.setColumnName("TRABAJADOR");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);


            JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${cod_zona}"));

            columnBinding2.setColumnName("ZONA");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding3 = this.jTableBinding.addColumnBinding(ELProperty.create("${unid_organizativa}"));

            columnBinding3.setColumnName("UNIDAD ORGANIZATIVA");
            columnBinding3.setColumnClass(String.class);
            columnBinding3.setEditable(false);

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();
           
        }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtTrabajadores = new javax.swing.JTable();
        jtfTrabajador = new javax.swing.JTextField();
        jbCancelar = new javax.swing.JButton();
        jbAceptar = new javax.swing.JButton();
        jtfDni = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lista de Trabajadores"); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtTrabajadores.setFont(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(jdVerTrabajadores.class).getFont("jtTrabajadores.font")); // NOI18N
        jtTrabajadores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DNI", "Trabajador", "Zona", "Unidad Organizativa"
            }
        ));
        jtTrabajadores.setName("jtTrabajadores"); // NOI18N
        jScrollPane1.setViewportView(jtTrabajadores);

        jtfTrabajador.setFont(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(jdVerTrabajadores.class).getFont("jLabel2.font")); // NOI18N
        jtfTrabajador.setName("jtfTrabajador"); // NOI18N
        jtfTrabajador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfTrabajadorActionPerformed(evt);
            }
        });
        jtfTrabajador.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfTrabajadorKeyPressed(evt);
            }
        });

        jbCancelar.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/close.gif"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CANCELAR"); // NOI18N
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jbAceptar.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12)); // NOI18N
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/options.gif"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText("ACEPTAR"); // NOI18N
        jbAceptar.setName("jbAceptar"); // NOI18N
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jtfDni.setFont(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(jdVerTrabajadores.class).getFont("jLabel2.font")); // NOI18N
        jtfDni.setName("jtfDni"); // NOI18N
        jtfDni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDniActionPerformed(evt);
            }
        });
        jtfDni.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDniKeyPressed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/replace.gif"))); // NOI18N
        jButton1.setText("CARGAR TODOS"); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(jdVerTrabajadores.class).getFont("jLabel2.font")); // NOI18N
        jLabel1.setText("DNI"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(org.jdesktop.application.Application.getInstance().getContext().getResourceMap(jdVerTrabajadores.class).getFont("jLabel2.font")); // NOI18N
        jLabel2.setText("USUARIO"); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfDni, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(56, 56, 56)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfTrabajador, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jbAceptar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbCancelar))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 626, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jbAceptar, jbCancelar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfDni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jtfTrabajador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbCancelar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jbAceptar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfTrabajadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfTrabajadorActionPerformed

        this.buscarTrabajador();
    }//GEN-LAST:event_jtfTrabajadorActionPerformed

    private void jtfTrabajadorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfTrabajadorKeyPressed
        if (this.jtfDni.getText().trim().length() > 0)
            this.jtfDni.setText("");
}//GEN-LAST:event_jtfTrabajadorKeyPressed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
         String cod_pedido = "";
         int filaElegida = this.jtTrabajadores.getSelectedRow();

         if (filaElegida < 0) {
              Utiles_MensajesDialogo.mensajeError("Debe elegir un Trabajador!");
              this.jtTrabajadores.requestFocus();
              return;
          }
         else{
              UsuarioTO tr  = (UsuarioTO)this.trabajadores_temp.get(filaElegida);
              jdMantenimientoTrabajador admin = new jdMantenimientoTrabajador(JOptionPane.getFrameForComponent(rootPane),false);
            try {
                Trabajador trabajador = GestionDelegate.getInstance().getTrabajador(tr.getDni());
                Persona persona = GestionDelegate.getInstance().getPersona((tr.getDni()));
                admin.setData(persona, trabajador);
            } catch (Exception ex) {
                Logger.getLogger(jdVerTrabajadores.class.getName()).log(Level.SEVERE, null, ex);
            }
              admin.setLocationRelativeTo(this);
                admin.setVisible(true);
             //this.dispose();
         }
    }//GEN-LAST:event_jbAceptarActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        
}//GEN-LAST:event_jbAceptarKeyPressed

    private void jtfDniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDniActionPerformed
             this.buscarTrabajador();
    }//GEN-LAST:event_jtfDniActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
         try {
            this.trabajadores_temp.clear();
            this.trabajadores = GestionSeguridadDelegate.getInstance().getListaTrabajadores();
             this.trabajadores_temp = this.trabajadores;
            this.actualizaTablaTrabajadores(this.trabajadores);
        } catch (Exception ex) {
            Logger.getLogger(jdVerTrabajadores.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jtfDniKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDniKeyPressed
        if (this.jtfDni.getText().trim().length() < 1)
            this.jtfDni.requestFocus();

    }//GEN-LAST:event_jtfDniKeyPressed

    /**
    * @param args the command line arguments
    */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JTable jtTrabajadores;
    private javax.swing.JTextField jtfDni;
    private javax.swing.JTextField jtfTrabajador;
    // End of variables declaration//GEN-END:variables

}
